var app = getApp()
var $ = require("../../utils/utils.js")
const config = require('../../config')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagesize: 10,
    pageno: 1,
    hasMoreData: true,
    activityList:[],
    isLogin:true
  },

  /***
   * methods
   * ****/
  getActivityList:function(){
    let data = {
        pagesize: this.data.pagesize,
        pageno: this.data.pageno,
        appid: app.appId,
    }
    let url = $.getUrl(config.activityList,data)
    $.get(url).then(data=>{

        var contentlistTem = this.data.activityList;
        var contentlist = data.detail.activity;
        if (contentlist.length < this.data.pagesize) {
            this.setData({
                activityList: contentlistTem.concat(contentlist),
                hasMoreData: false
            })
        } else {
            this.setData({
                activityList: contentlistTem.concat(contentlist),
                hasMoreData: true,
            });
        }
    })
  },

  enterActivity:function(e){
      console.log('e--------',e)
      let that = this
      let activityid = e.currentTarget.dataset.activityid
      let jumpurl = e.currentTarget.dataset.jumpurl
      if (!wx.getStorageSync("isLogin")) {
         
          if (e.detail.errMsg == 'getPhoneNumber:ok') {
              app.aesPhoneNumber(e.detail.iv, e.detail.encryptedData).then(data => {
                  that.jumpPath(activityid, jumpurl)
              })
          } else if (e.type == "getphonenumber") {
              $.goto(config.page.login)
            //   that.jumpPath(activityid, jumpurl)
          }
      } else {
          
          that.jumpPath(activityid, jumpurl)
      }  
      
  },

  // 判断跳转网页/小程序
  jumpPath: function (activityid, jumpurl){
      this.setData({
          isLogin:true
      })
      console.log(activityid, jumpurl)
      if (jumpurl.indexOf('https') != -1) {
          app.globalData.webview = {
              url: jumpurl,
              title: '活动'
          };
          $.goto(config.page.webview + '?activityId=' + activityid, false);
      } else {
          $.goto(jumpurl + '?activityId=' + activityid)
      }
  },  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      let islogin = wx.getStorageSync("isLogin")
      this.setData({
          isLogin: islogin
      })
      this.getActivityList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      let islogin = wx.getStorageSync("isLogin")
      this.setData({
          isLogin: islogin
      })
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1];
      var prevPage = pages[pages.length - 2];
      wx.setStorageSync("activityAlreayCount", prevPage.data.activityCount)
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      var that = this;
      if (that.data.hasMoreData) {
          that.setData({
              pageno: that.data.pageno + 1
          })
          that.getActivityList();
      } else {
          wx.showToast({
              title: '没有更多数据',
          });
          setTimeout(function () {
              wx.hideToast();
          }, 2000);
      }  
  },
})